 
/**
 * name:        提交订单
 * author:      wuwq & lyj
 * Time:        18/01/26
 * Copyright:   bgemall.com
 */

 $(function(){
     $('#checkbox').attr('checked',true)
    if($.cookie('user')!=''&&$.cookie('user')!=null){
        var url=window.location.search;
        var id = url.substring(url.lastIndexOf('=')+1, url.length);
        // 是否登录
        strs=$.cookie('user').split(",")
        var token=''
        var phone_binding=''
        for(var i in strs){
            token=strs[6]
            phone_binding=strs[2]
        } 
        var is = ''
        var site=''

        var token=''
        isLogin();      
        getToken();     // 获取/验证token 
                // 导航栏显示已登录
                function isLogin(){
                }
            
                // 获取/验证token
                function getToken(){
                    strs=$.cookie('user').split(",");
                    for(var i in strs){ 
                        $('.nike_name').html(strs[1])
                        $('.avater img').attr('src',strs[3])
                        token=strs[6]
                    }
                }
            
                // 获取收货地址
                // function getAddress(){
                    $.ajax({
                        type:'post',
                        url:new_url+'shop/member/getShippingAddressList',
                        dataType:'json',
                        data:{token:token},  
                        success:function(e){ 
                            var address = $("#address").html();
                            if(e.data.length=='0'){
                                $('.site_list_list').remove()
                                $('.site_list_no').show()                              
                            }
                            else{
                                $('.site_list_no').remove()
                                $('.site_list_list').show()
                                for (var i=0; i<e.data.length; i++){
                                    if(e.data[i].isDefault == 1){
                                        e.data[i].isDefaultTxt='默认收货地址'
                                    }else{
                                        e.data[i].isDefaultTxt='设为默认地址'
                                    }
                                    var addressListcont=template(
                                        address, {
                                            addressList: [{
                                                addressee:e.data[i].addressee,
                                                phone:e.data[i].phone,
                                                address:e.data[i].address,
                                                areaInfo:e.data[i].areaInfo,
                                                isDefault:e.data[i].isDefault,
                                                isDefaultTxt:e.data[i].isDefaultTxt,
                                                addressId:e.data[i].addressId,
                                                provinceId:e.data[i].provinceId,
                                                cityId:e.data[i].cityId,
                                                areaId:e.data[i].areaId,
                                            }]
                                        }
                                    );
                                    $('.address_wrap ul').append(addressListcont);  
                                }
                                $('.address_wrap ul li').each(function(index,t){
                                    is=$(t).find('.address_set_default').attr('data-isdefault') 
                                    if(is=='1'){ 
                                        $(t).addClass('active')
                                        $(t).find('.delete_address').remove()
                                        var te=$(t).find('.address_info').text()
                                        $('.pay_speci li').eq(1).find('em').text(te)
                                    }
                                    site=$('.address_wrap ul li.active').attr('data-addres-id')
                                    $('.address_wrap ul li').click(function(){
                                        $('.address_wrap ul li').removeClass('active')
                                        $(this).addClass('active')
                                        site=$(this).attr('data-addres-id')
                                        var te=$(this).find('.address_info').text()
                                        $('.pay_speci li').eq(1).find('em').text(te) 
                                        if($.cookie('my_cookie')!=null&&$.cookie('my_cookie')!=''){
                                            m()
                                            return false
                                        }
                                        if($.cookie('goods_user')!=null&&$.cookie('goods_user')!=''){
                                            g()
                                            return false
                                        }
                                    }) 
                                })
                            }
                            isDefault(token,URL);
                            deleteAddress(token,URL);
                            editAddress();
                            addAddress();
                        }  
                    });
                // }
            
                // 编辑收货地址
                function editAddress(){
                    $(".edit_address").click(function(){
                        var addressId = $(this).parents().find("li").attr('data-addres-id');
                        var addressee = $(this).parents().find(".address_tit .address_name").html();
                        var areaId = $(this).attr('data-areaId');
                        var cityId = $(this).attr('data-cityId');
                        var provinceId = $(this).attr('data-provinceId');
                        var areaInfo = $(this).parents().find(".address_info").attr('data-area-info');
                        var address = $(this).parents().find(".address_info").attr('data-address');
                        var phone = $(this).parents().find(".address_tit .address_phone").html();
                        var isDefault = 0;
                        editAddressPop(addressId,addressee,areaId,cityId,provinceId,areaInfo,address,phone,isDefault,token,URL)
                        
                    });
                }
            
                // 新增收货地址
                function addAddress(){
                    $(".add_address").click(function(){
                        addAddressPop(token,URL);
                    })
                }        
        // 商品详情直接购买
        if($.cookie('goods_user')!=null&&$.cookie('goods_user')!=''){
                user=$.cookie('goods_user').split("&")
                var buyNum='' 
                var goodsId ='' 
                var isFromCart=''
                for (var t in user){ 
                    buyNum=user[0]
                    goodsId=user[1]
                    isFromCart=user[2]
                }
                function g(){
                    var m=0 
                    var z_totalWeight='';
                    
                    var length_1=0
                    var length_2=0
                    var z_un=0
                    var z_st=0
                    var z_un1=0
                    var z_st1=0
                    var z_sn1=0
                    $.ajax({
                        url:new_url+'shop/buy/getSettleGoodsList',
                        type:'post',
                        dataType:'json',
                        data:{token:token,buyNum:buyNum,goodsId:goodsId,isFromCart:'0'},
                        success:function(e){ 
                            var clearing_bg=$('#clearing_bg').html()
                            var clearing_bg1=$('#clearing_bg1').html()
                            for(var le=0; le<e.data.length; le++){
                                var z_unitPrice=0
                                // 当前供应商商品的总重
                                var totalWeight_z=0
                                var z_additionalWeightFreight=0
                                var unitPrice_this=0
                                var store1=0
            
                                    for(var l=0; l<e.data[le].goodsList.length; l++){
                        //判断是否达到免运费额度
                        z_unitPrice+=parseFloat(e.data[le].goodsList[l].totalPrice)   
                        var seven_text=document.getElementsByClassName('seven_text')
                        var dl_bg=document.getElementsByClassName('dl_bg')
                        $('.dl_bg').each(function(){
                            $(this).attr('data-isfreefreight',$(this).find('dd').attr('data-isfreefreight'))
                        })
                        
                        if(z_unitPrice>=e.data[le].storefreeFreight&&e.data[le].storefreeFreight!='0.00' || e.data[le].goodsList[l].isFreeFreight=='0'){
                            dl_bg[le].setAttribute("class", "dl_bg yes");   
                        } 
                        //运费
                        if(e.data[le].goodsList[l].isFreeFreight!='0'){
                            // 当前重量 
                            var totalWeight=parseFloat(e.data[le].goodsList[l].totalWeight)
                            //全部重量
                            totalWeight_z+=totalWeight
                            var totalWeight_z1= Math.ceil(totalWeight_z) 
                        for(var store=0; store<e.data[le].storefreightTemplate.length; store++){
                            function store(){
                                if($('.site_list ul li').hasClass('active')){
                                    var provinceid=$('.site_list ul li.active').find('.edit_address').attr('data-provinceid')
                                    //当前选择城市id
                                    var cityid=$('.site_list ul li.active').find('.edit_address').attr('data-cityid')
                                    // 循环运费模板城市id
                                    for(var t=0; t<e.data[le].storefreightTemplate.length; t++){
                                        var cityId2=e.data[le].storefreightTemplate[t].cityIds.split(',')
                                        var additionalWeightFreight=parseFloat(e.data[le].storefreightTemplate[t].additionalWeightFreight)
                                        // 匹配选择城市id与运费模板城市id是否相同
                                        for(var city=0; city<cityId2.length; city++){
                                            if(cityid==cityId2[city]){ 
                                                $('.dl_bg').attr('data-isDefault','2') 
                                                // 获取首重运费
                                            z_totalWeight=parseInt(e.data[le].storefreightTemplate[t].firstWeightFreight)
                                            // console.log(z_totalWeight)
                                                //获取最大首重重量
                                                var w=z_totalWeight
                                                store1=w
                                                // 首重+goodsSpec
                                                unitPrice_this=(store1+z_unitPrice) 
                                                // 判断当前供应商商品的总重 跟 首重对比
                                                if(e.data[le].storefreightTemplate[t].firstWeight >= totalWeight_z1){
                                                    z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[t].firstWeight)*additionalWeightFreight
                                                    if($('.dl_bg').length>=1){
                                                        $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                        $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                    }                                                 
                                                }
                                                // 计算超出重量运费加上首重运费
                                                else{
                                                    //总重-首重 超出首重重量*续重金额 
                                                    z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[t].firstWeight)*additionalWeightFreight
                                                    if($('.dl_bg').length>=1){
                                                        $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                        $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                    }                                                                   
                                                } 
                                                return
                                            }
                                            // // 没有就选择全国城市运费模板
                                            else{
                                                $('.dl_bg').attr('data-isDefault','1')
                                                if($('.dl_bg').attr('data-isDefault')=='1'){ 
                                        // 获取首重运费
                                        z_totalWeight=parseInt(e.data[le].storefreightTemplate[0].firstWeightFreight) 
                                        //获取最大首重重量
                                        var w=z_totalWeight
                                        store1=w
                                        // 首重+goodsSpec
                                        unitPrice_this=(store1+z_unitPrice) 
                                        // 判断当前供应商商品的总重 跟 首重对比 
                                        if(e.data[le].storefreightTemplate[0].firstWeight >= totalWeight_z1){
                                            if($('.dl_bg').length>=1){
                                                $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                            }
                                        }
                                        
                                        // 计算超出重量运费加上首重运费
                                            else{
                                            var add=parseFloat(e.data[le].storefreightTemplate[0].additionalWeightFreight)
                                            //总重-首重 超出首重重量*续重金额 
                                            z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[0].firstWeight)*add 
                                            if($('.dl_bg').length>=1){
                                                $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                            }                                                                   
                                            }                 
                                                }
                                            }
                                        }
                                    }
                                }
                            }store()
                        }
                        }       
                        else{ 
                            $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                            $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight) 
                        }   
      
                       

                                    $('.add_up li span').eq(1).text('暂无')
                                    $('.cart_list dd').each(function(index,t){
                                        $('.g_num').text(index+1)
                                        $('.cart_num em').text(index+1+'件')
                                    }) 
                                }
                                var store=0
                                var un=0
                                
                                $('.dl_bg').each(function(index,t){
                                    var to=0
                                    $(t).find('dd').each(function(){
                                        to+=parseFloat($(this).find('.five em').text()) 
                                        length_1+=parseFloat($(this).find('.five em').text())
                                    }) 
                                    if(to>=parseFloat($(t).attr('data-storefreefreight'))&&$(t).attr('data-storefreefreight')!='0.00'){
                                        $(t).attr('data-unitprice',to)
                                        $(t).attr('data-store','0') 
                                    }
                                    store+=parseFloat($(t).attr('data-store'))
                                    un+=parseFloat($(t).attr('data-unitprice'))
                                    $('.store1 i').text(store.toFixed(2))
                                    $('.unitPrice').text('￥'+(length_1+store).toFixed(2)) 
                                })
                            }
                        }
                    })
                    }        
            goods_user()
        }
        // cart购买
        if($.cookie('my_cookie')!=null&&$.cookie('my_cookie')!=''){
            function m(){
                var m=0 
                var z_totalWeight='';
                var length_2=0
                var z_un=0
                var z_st=0
                var z_un1=0
                var z_st1=0
                var z_sn1=0
                $.ajax({
                    url:new_url+'shop/buy/getSettleGoodsList',
                    type:'post',
                    dataType:'json',
                    data:{token:token,cartId:$.cookie('my_cookie'),isFromCart:'1'},
                    success:function(e){ 
                        var clearing_bg=$('#clearing_bg').html()
                        var clearing_bg1=$('#clearing_bg1').html()
                            for(var le=0; le<e.data.length; le++){
                                var length_1=0
                                var z_unitPrice=0
                                // 当前供应商商品的总重
                                var totalWeight_z=0
                                var z_additionalWeightFreight=0
                                var unitPrice_this=0
                                var store1=0

                                for(var l=0; l<e.data[le].goodsList.length; l++){                                       
                                
                                        //判断是否达到免运费额度
                                        z_unitPrice+=parseFloat(e.data[le].goodsList[l].totalPrice)     
                                        var seven_text=document.getElementsByClassName('seven_text')
                                        var dl_bg=document.getElementsByClassName('dl_bg')
                                        $('.dl_bg').each(function(){
                                            $(this).attr('data-isfreefreight',$(this).find('dd').attr('data-isfreefreight'))
                                        })
                                        
                                        if(z_unitPrice>=e.data[le].storefreeFreight&&e.data[le].storefreeFreight!='0.00' || e.data[le].goodsList[l].isFreeFreight=='0'){
                                            dl_bg[le].setAttribute("class", "dl_bg yes");  
                                        }
                                        //运费
                                        if(e.data[le].goodsList[l].isFreeFreight!='0'){
                                            // 当前重量 
                                            var totalWeight=parseFloat(e.data[le].goodsList[l].totalWeight)
                                            //全部重量
                                            totalWeight_z+=totalWeight
                                            var totalWeight_z1= Math.ceil(totalWeight_z) 
                                        for(var store=0; store<e.data[le].storefreightTemplate.length; store++){
                                            function store(){
                                                if($('.site_list ul li').hasClass('active')){
                                                    var provinceid=$('.site_list ul li.active').find('.edit_address').attr('data-provinceid')
                                                    //当前选择城市id
                                                    var cityid=$('.site_list ul li.active').find('.edit_address').attr('data-cityid')
                                                    // 循环运费模板城市id
                                                    for(var t=0; t<e.data[le].storefreightTemplate.length; t++){
                                                        var cityId2=e.data[le].storefreightTemplate[t].cityIds.split(',')
                                                        var additionalWeightFreight=parseFloat(e.data[le].storefreightTemplate[t].additionalWeightFreight)
                                                        // 匹配选择城市id与运费模板城市id是否相同
                                                        for(var city=0; city<cityId2.length; city++){
                                                            if(cityid==cityId2[city]){ 
                                                                $('.dl_bg').attr('data-isDefault','2') 
                                                                // 获取首重运费
                                                            z_totalWeight=parseInt(e.data[le].storefreightTemplate[t].firstWeightFreight)
                                                                //获取最大首重重量
                                                                var w=z_totalWeight
                                                                store1=w
                                                                // 首重+goodsSpec
                                                                unitPrice_this=(store1+z_unitPrice)
                                                                // 判断当前供应商商品的总重 跟 首重对比
                                                                if(e.data[le].storefreightTemplate[t].firstWeight >= totalWeight_z1){
                                                                    z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[t].firstWeight)*additionalWeightFreight
                                                                    if($('.dl_bg').length>=1){
                                                                        $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                                        $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                                    }                                                 
                                                                }
                                                                // 计算超出重量运费加上首重运费
                                                                 else{
                                                                    //总重-首重 超出首重重量*续重金额 
                                                                    z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[t].firstWeight)*additionalWeightFreight
                                                                    if($('.dl_bg').length>=1){
                                                                        $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                                        $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                                    }                                                                   
                                                                 } 
                                                                 return
                                                            }
                                                            // // 没有就选择全国城市运费模板
                                                            else{
                                                                $('.dl_bg').attr('data-isDefault','1')
                                                                if($('.dl_bg').attr('data-isDefault')=='1'){
                                                        // 获取首重运费
                                                        z_totalWeight=parseInt(e.data[le].storefreightTemplate[0].firstWeightFreight)
                                                        //获取最大首重重量
                                                        var w=z_totalWeight
                                                        store1=w
                                                        // 首重+goodsSpec
                                                        unitPrice_this=(store1+z_unitPrice)
                                                        // 判断当前供应商商品的总重 跟 首重对比
                                                        if(e.data[le].storefreightTemplate[0].firstWeight >= totalWeight_z1){
                                                            if($('.dl_bg').length>=1){
                                                                $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                                $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                            }
                                                        }
                                                        
                                                        // 计算超出重量运费加上首重运费
                                                            else{
                                                            var add=parseFloat(e.data[le].storefreightTemplate[0].additionalWeightFreight)
                                                            //总重-首重 超出首重重量*续重金额 
                                                            z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[0].firstWeight)*add
                                                            if($('.dl_bg').length>=1){
                                                                $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                                $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                            }                                                                   
                                                            }                 
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }store()
                                        }
                                        }       
                                        else{
                                            $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                            $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                        }                                                                                    
                                        $('.cart_list dd').each(function(index,t){
                                            $('.g_num').text(index+1)
                                            $('.cart_num em').text(index+1+'件') 
                                        }) 
                                    }
                                    }
                                    var store=0
                                    var un=0
                                    
                                    $('.dl_bg').each(function(index,t){
                                        var to=0
                                        $(t).find('dd').each(function(){
                                            to+=parseFloat($(this).find('.five em').text()) 
                                            length_1+=parseFloat($(this).find('.five em').text())
                                        }) 
                                        if(to>=parseFloat($(t).attr('data-storefreefreight')) && $(t).attr('data-storefreefreight')!='0.00'){
                                            $(t).attr('data-unitprice',to)
                                            $(t).attr('data-store','0')
                                        }
                                        store+=parseFloat($(t).attr('data-store'))
                                        un+=parseFloat($(t).attr('data-unitprice'))
                                        $('.store1 i').text(store.toFixed(2))
                                        $('.unitPrice').text('￥'+(length_1+store).toFixed(2))
                                            //有运费 无地址 显示NaN的时候 为0.00
                                        if($('.store1 i').text()=='NaN'){
                                            $('.store1 i').text('0.00')
                                        }
                                        if($('.unitPrice').text()=='￥NaN'){
                                            $('.unitPrice').text('￥0.00')
                                        }
                                    })
                        
                    }
                })
                }  

            var cartId2=''
            var buyNum=''
            var goodsId=''
            var name=''
            var spec=''
            var isFromCart=''
            var h=0
                    // 购物车购买
                    function cart(){
                        var m=0 
                        var z_totalWeight='';
                        
                        
                        var length_2=0
                        var z_un=0
                        var z_st=0
                        var z_un1=0
                        var z_st1=0
                        var z_sn1=0
                        
                        $.ajax({
                            url:new_url+'shop/buy/getSettleGoodsList',
                            type:'post',
                            dataType:'json',
                            data:{token:token,cartId:$.cookie('my_cookie'),isFromCart:'1'},
                            success:function(e){ 
                                var clearing_bg=$('#clearing_bg').html()
                                var clearing_bg1=$('#clearing_bg1').html()
                                    for(var le=0; le<e.data.length; le++){
                                        var length_1=0
                                        var z_unitPrice=0
                                        // 当前供应商商品的总重
                                        var totalWeight_z=0
                                        var z_additionalWeightFreight=0
                                        var unitPrice_this=0
                                        var store1=0
                                        var clearing1=template(
                                            clearing_bg1,{
                                                list:[{
                                                    storefreeFreight:e.data[le].storefreeFreight
                                                }]
                                            }
                                        )
                                        $('#clearing').append(clearing1)
                                        
                                        for(var l=0; l<e.data[le].goodsList.length; l++){
                                        var img = e.data[le].goodsList[l].image;
                                        var img1 = img.replace('dangmai', 'bgemall');
                                        var clearing=template(
                                            clearing_bg,{
                                                list:[{
                                                    image:img1,
                                                    name:e.data[le].goodsList[l].name,
                                                    goodsSpec:e.data[le].goodsList[l].goodsSpec,
                                                    unitPrice:e.data[le].goodsList[l].unitPrice,
                                                    buyNum:e.data[le].goodsList[l].buyNum,
                                                    totalPrice:e.data[le].goodsList[l].totalPrice,
                                                    goodsId:e.data[le].goodsList[l].goodsId,
                                                    totalWeight:e.data[le].goodsList[l].totalWeight+'kg',
                                                    isFreeFreight:e.data[le].goodsList[l].isFreeFreight
                                                }]
                                            }
                                        )
                                        $('.dl_bg').eq(le).append(clearing)
                                        // 规格为空显示默认
                                        if(e.data[le].goodsList[l].goodsSpec==null){
                                            $('.speci em').text('规格：默认')
                                        }
                                        //企业会员优惠
                                        $('.add_up li span').eq(1).text('暂无')
                                        //判断是否达到免运费额度
                                        z_unitPrice+=parseFloat(e.data[le].goodsList[l].totalPrice)  
                                        var seven_text=document.getElementsByClassName('seven_text')
                                        var dl_bg=document.getElementsByClassName('dl_bg')
                                        $('.dl_bg').each(function(){
                                            $(this).attr('data-isfreefreight',$(this).find('dd').attr('data-isfreefreight'))
                                        })
                                        
                                        if(z_unitPrice>=e.data[le].storefreeFreight&&e.data[le].storefreeFreight!='0.00' || e.data[le].goodsList[l].isFreeFreight=='0'){
                                            dl_bg[le].setAttribute("class", "dl_bg yes");  
                                        }
                                        //运费
                                        if(e.data[le].goodsList[l].isFreeFreight!='0'){
                                            // 当前重量 
                                            var totalWeight=parseFloat(e.data[le].goodsList[l].totalWeight)
                                            //全部重量
                                            totalWeight_z+=totalWeight
                                            var totalWeight_z1= Math.ceil(totalWeight_z)
                                            // console.log(totalWeight_z1)
                                        for(var store=0; store<e.data[le].storefreightTemplate.length; store++){
                                            function store(){
                                                if($('.site_list ul li').hasClass('active')){
                                                    var provinceid=$('.site_list ul li.active').find('.edit_address').attr('data-provinceid')
                                                    //当前选择城市id
                                                    var cityid=$('.site_list ul li.active').find('.edit_address').attr('data-cityid')
                                                    // 循环运费模板城市id
                                                    for(var t=0; t<e.data[le].storefreightTemplate.length; t++){
                                                        var cityId2=e.data[le].storefreightTemplate[t].cityIds.split(',')
                                                        var additionalWeightFreight=parseFloat(e.data[le].storefreightTemplate[t].additionalWeightFreight)
                                                        // 匹配选择城市id与运费模板城市id是否相同
                                                        for(var city=0; city<cityId2.length; city++){
                                                            if(cityid==cityId2[city]){ 
                                                                $('.dl_bg').attr('data-isDefault','2') 
                                                                // 获取首重运费
                                                            z_totalWeight=parseInt(e.data[le].storefreightTemplate[t].firstWeightFreight)
                                                                //获取最大首重重量
                                                                var w=z_totalWeight
                                                                store1=w
                                                                // 首重+goodsSpec
                                                                unitPrice_this=(store1+z_unitPrice)
                                                                // 判断当前供应商商品的总重 跟 首重对比
                                                                if(e.data[le].storefreightTemplate[t].firstWeight >= totalWeight_z1){
                                                                    z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[t].firstWeight)*additionalWeightFreight
                                                                    if($('.dl_bg').length>=1){
                                                                        $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                                        $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                                    }                                                 
                                                                }
                                                                // 计算超出重量运费加上首重运费
                                                                 else{
                                                                    //总重-首重 超出首重重量*续重金额 
                                                                    z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[t].firstWeight)*additionalWeightFreight
                                                                    if($('.dl_bg').length>=1){
                                                                        $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                                        $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                                    }                                                                   
                                                                 } 
                                                                 return
                                                            }
                                                            // // 没有就选择全国城市运费模板
                                                            else{
                                                                $('.dl_bg').attr('data-isDefault','1')
                                                                if($('.dl_bg').attr('data-isDefault')=='1'){
                                                        // 获取首重运费
                                                        z_totalWeight=parseInt(e.data[le].storefreightTemplate[0].firstWeightFreight)
                                                        //获取最大首重重量
                                                        var w=z_totalWeight
                                                        store1=w
                                                        // 首重+goodsSpec
                                                        unitPrice_this=(store1+z_unitPrice)
                                                        // 判断当前供应商商品的总重 跟 首重对比
                                                        if(e.data[le].storefreightTemplate[0].firstWeight >= totalWeight_z1){
                                                            if($('.dl_bg').length>=1){
                                                                $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                                $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                            }
                                                        }
                                                        
                                                        // 计算超出重量运费加上首重运费
                                                            else{
                                                            var add=parseFloat(e.data[le].storefreightTemplate[0].additionalWeightFreight)
                                                            //总重-首重 超出首重重量*续重金额 
                                                            z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[0].firstWeight)*add 
                                                            if($('.dl_bg').length>=1){
                                                                $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                                $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                            }                                                                   
                                                            }                 
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }store()
                                        }
                                        }       
                                        else{
                                            $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                            $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                        }                                                                                    
                                        $('.cart_list dd').each(function(index,t){
                                            $('.g_num').text(index+1)
                                            $('.cart_num em').text(index+1+'件') 
                                        }) 
                                    }
                                    }
                                    var store=0
                                    var un=0
                                    
                                    $('.dl_bg').each(function(index,t){
                                        var to=0
                                        $(t).find('dd').each(function(){
                                            to+=parseFloat($(this).find('.five em').text()) 
                                            length_1+=parseFloat($(this).find('.five em').text())
                                        })
                                        if(to>=parseFloat($(t).attr('data-storefreefreight'))&&$(t).attr('data-storefreefreight')!='0.00'){
                                            $(t).attr('data-unitprice',to)
                                            $(t).attr('data-store','0')
                                        }
                                        store+=parseFloat($(t).attr('data-store'))
                                        un+=parseFloat($(t).attr('data-unitprice'))
                                        $('.store1 i').text(store.toFixed(2))
                                        $('.unitPrice').text('￥'+(length_1+store).toFixed(2)) 
                                            //有运费 无地址 显示NaN的时候 为0.00
                                            if($('.store1 i').text()=='NaN'){
                                                $('.store1 i').text('0.00')
                                            }
                                            if($('.unitPrice').text()=='￥NaN'){
                                                $('.unitPrice').text('￥0.00')
                                            }
                                    })
                            }
                           
                        })

                        // 支付
                        $('#pay_ok').click(function(){
//                      	暂时停止支付功能
//                      	tc('结算失败，请刷新或者重新抢购，谢谢！')
//                      	setTimeout(function(){
//								window.location.href='../../index.html'
//							},5000)
//                      	
//                      	return false;
                            if(phone_binding!=null&&phone_binding!=''){
                            // getAddress()
                            var message=$('#message').val()
                            var addressId=site 
                            var freight=parseFloat($('.store1 i').text())
                            $.ajax({
                                url:new_url+'shop/buy/createOrder',
                                data:{token:token,message:message,isFromCart:'1',addressId:addressId,cartId:$.cookie('my_cookie'),freight:freight},
                                dataType:'json',
                                type:'post',
                                success:function(n){
                                    if(n.message=='订单提交成功'){
                                        tc(n.message)
                                        var ok=n.data.payNumber+'&'+n.data.orderPrice
                                        $.cookie('ok',ok,{path:'/'});
                                        window.location.href='/src/cart/pay.html'
                                    }
                                    else{
                                        if($('.site_list_no').show()){
                                            tc('请选择收货地址')
                                        }
                                        else{
                                            tc(n.message)
                                        }  
                                    }
                                }
                            })
//                          return false
                            }
                            else{
                                // tc('请绑定手机号');
                                var this_u=window.location.href
                                $('.popover_m1 , .site_bg1').fadeIn()
                                $('.gb_01, .popover_m1').click(function(){
                                    $('.popover_m1, .site_bg1').fadeOut()
                                })
                            }
                        }) 
            } cart()
        }

    // 商品详情直接购买    
    function goods_user(){
        var m=0 
        var z_totalWeight='';
        
        var length_1=0
        var length_2=0
        var z_un=0
        var z_st=0
        var z_un1=0
        var z_st1=0
        var z_sn1=0
        $.ajax({
            url:new_url+'shop/buy/getSettleGoodsList',
            type:'post',
            dataType:'json',
            data:{token:token,buyNum:buyNum,goodsId:goodsId,isFromCart:'0'},
            success:function(e){ 
                var clearing_bg=$('#clearing_bg').html()
                var clearing_bg1=$('#clearing_bg1').html()
                for(var le=0; le<e.data.length; le++){
                    var z_unitPrice=0
                    // 当前供应商商品的总重
                    var totalWeight_z=0
                    var z_additionalWeightFreight=0
                    var unitPrice_this=0
                    var store1=0
                    var clearing1=template(
                        clearing_bg1,{
                            list:[{
                                storefreeFreight:e.data[le].storefreeFreight
                            }]
                        }
                    )
                    $('#clearing').append(clearing1)
                        for(var l=0; l<e.data[le].goodsList.length; l++){
                        var img = e.data[le].goodsList[l].image;
                        var img1 = img.replace('dangmai', 'bgemall');
                        var clearing=template(
                            clearing_bg,{
                                list:[{
                                    image:img1,
                                    name:e.data[le].goodsList[l].name,
                                    goodsSpec:e.data[le].goodsList[l].goodsSpec,
                                    unitPrice:e.data[le].goodsList[l].unitPrice,
                                    buyNum:e.data[le].goodsList[l].buyNum,
                                    totalPrice:e.data[le].goodsList[l].totalPrice,
                                    goodsId:e.data[le].goodsList[l].goodsId,
                                    totalWeight:e.data[le].goodsList[l].totalWeight+'kg'
                                }]
                            }
                        )
                        $('.dl_bg').eq(le).append(clearing)
                        // 规格为空显示默认
                        if(e.data[le].goodsSpec==null){
                            $('.speci em').text('规格：默认')
                        }
                        //判断是否达到免运费额度
                        z_unitPrice+=parseFloat(e.data[le].goodsList[l].totalPrice)  
                        var seven_text=document.getElementsByClassName('seven_text')
                        var dl_bg=document.getElementsByClassName('dl_bg')
                        $('.dl_bg').each(function(){
                            $(this).attr('data-isfreefreight',$(this).find('dd').attr('data-isfreefreight'))
                        })
                        
                        if(z_unitPrice>=e.data[le].storefreeFreight&&e.data[le].storefreeFreight!='0.00' || e.data[le].goodsList[l].isFreeFreight=='0'){
                            dl_bg[le].setAttribute("class", "dl_bg yes");  
                        }
                        //运费
                        if(e.data[le].goodsList[l].isFreeFreight!='0'){
                            // 当前重量 
                            var totalWeight=parseFloat(e.data[le].goodsList[l].totalWeight)
                            //全部重量
                            totalWeight_z+=totalWeight
                            var totalWeight_z1= Math.ceil(totalWeight_z); 
                        for(var store=0; store<e.data[le].storefreightTemplate.length; store++){
                            function store(){
                                if($('.site_list ul li').hasClass('active')){
                                    var provinceid=$('.site_list ul li.active').find('.edit_address').attr('data-provinceid')
                                    //当前选择城市id
                                    var cityid=$('.site_list ul li.active').find('.edit_address').attr('data-cityid')
                                    // 循环运费模板城市id
                                    for(var t=0; t<e.data[le].storefreightTemplate.length; t++){
                                        var cityId2=e.data[le].storefreightTemplate[t].cityIds.split(',')
                                        var additionalWeightFreight=parseFloat(e.data[le].storefreightTemplate[t].additionalWeightFreight)
                                        // 匹配选择城市id与运费模板城市id是否相同
                                        for(var city=0; city<cityId2.length; city++){
                                            if(cityid==cityId2[city]){ 
                                                $('.dl_bg').attr('data-isDefault','2') 
                                                // 获取首重运费
                                            z_totalWeight=parseInt(e.data[le].storefreightTemplate[t].firstWeightFreight)
                                                //获取最大首重重量
                                                var w=z_totalWeight
                                                store1=w
                                                // 首重+goodsSpec
                                                unitPrice_this=(store1+z_unitPrice)
                                                // 判断当前供应商商品的总重 跟 首重对比
                                                if(e.data[le].storefreightTemplate[t].firstWeight >= totalWeight_z1){
                                                    z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[t].firstWeight)*additionalWeightFreight
                                                    if($('.dl_bg').length>=1){
                                                        $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                        $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                    }                                                 
                                                }
                                                // 计算超出重量运费加上首重运费
                                                else{
                                                    //总重-首重 超出首重重量*续重金额 
                                                    z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[t].firstWeight)*additionalWeightFreight
                                                    if($('.dl_bg').length>=1){
                                                        $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                        $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                                    }                                                                   
                                                } 
                                                return
                                            }
                                            // // 没有就选择全国城市运费模板
                                            else{
                                                $('.dl_bg').attr('data-isDefault','1')
                                                if($('.dl_bg').attr('data-isDefault')=='1'){
                                        // 获取首重运费
                                        z_totalWeight=parseInt(e.data[le].storefreightTemplate[0].firstWeightFreight)
                                        //获取最大首重重量
                                        var w=z_totalWeight
                                        store1=w
                                        // 首重+goodsSpec
                                        unitPrice_this=(store1+z_unitPrice)
                                        // 判断当前供应商商品的总重 跟 首重对比
                                        if(e.data[le].storefreightTemplate[0].firstWeight >= totalWeight_z1){
                                            if($('.dl_bg').length>=1){
                                                $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                            }
                                        }
                                        
                                        // 计算超出重量运费加上首重运费
                                            else{
                                            var add=parseFloat(e.data[le].storefreightTemplate[0].additionalWeightFreight)
                                            //总重-首重 超出首重重量*续重金额 
                                            z_additionalWeightFreight=((Math.ceil(totalWeight_z1))-e.data[le].storefreightTemplate[0].firstWeight)*add
                                            if($('.dl_bg').length>=1){
                                                $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                                                $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                                            }                                                                   
                                            }                 
                                            }
                                            }
                                        }
                                    }
                                }
                            }store()
                        }
                        }       
                        else{
                            $('.dl_bg').eq(le).attr('data-unitPrice',z_additionalWeightFreight+unitPrice_this)
                            $('.dl_bg').eq(le).attr('data-store',store1+z_additionalWeightFreight)
                        }                       
  
                    }
                    $('.add_up li span').eq(1).text('暂无')
                    $('.cart_list dd').each(function(index,t){
                        $('.g_num').text(index+1)
                        $('.cart_num em').text(index+1+'件')
                    }) 
                    var store=0
                    var un=0
                    
                    $('.dl_bg').each(function(index,t){
                        var to=0
                        $(t).find('dd').each(function(){
                            to+=parseFloat($(this).find('.five em').text()) 
                            length_1+=parseFloat($(this).find('.five em').text())
                        })
                        if(to>=parseFloat($(t).attr('data-storefreefreight'))&&$(t).attr('data-storefreefreight')!='0.00'){
                            $(t).attr('data-unitprice',to)
                            $(t).attr('data-store','0')
                        }
                        store+=parseFloat($(t).attr('data-store'))
                        un+=parseFloat($(t).attr('data-unitprice'))
                        $('.store1 i').text(store.toFixed(2))
                        $('.unitPrice').text('￥'+(length_1+store).toFixed(2))
                            //有运费 无地址 显示NaN的时候 为0.00
                        if($('.store1 i').text()=='NaN'){
                            $('.store1 i').text('0.00')
                        }
                        if($('.unitPrice').text()=='￥NaN'){
                            $('.unitPrice').text('￥0.00')
                        }
                    })
                }
            }
        })

        // 支付
        
        $('#pay_ok').click(function(){
        	//                      	暂时停止支付功能
//                      	tc('结算失败，请刷新或者重新抢购，谢谢！')
//                      	setTimeout(function(){
//								window.location.href='../../index.html'
//							},5000)
//                      	return false;
            if(phone_binding!=null&&phone_binding!=''){
                var message=$('#message').val()
                var addressId=site      
                var freight=parseFloat($('.store1 i').text())     
                $.ajax({
                    url:new_url+'shop/buy/createOrder',
                    data:{token:token,buyNum:buyNum,goodsId:goodsId,isFromCart:'0',freight:freight,addressId:addressId,message:message},
                    dataType:'json',
                    type:'post',
                    success:function(n){
                        if(n.message=='订单提交成功'){                       
                            tc(n.message)
                            var ok=n.data.payNumber+'&'+n.data.orderPrice
                            $.cookie('ok',ok,{path:'/'});
                            setTimeout(function(){
                                window.location.href='/src/cart/pay.html'
                            },400)
                        }
                        else{
                            if($('.site_list_no').show()){
                                tc('请选择收货地址')
                            }
                            else{
                                tc(n.message)
                            }
                            
                        }
                    }
                })
            }
            else{
                var this_u=window.location.href
                $('.popover_m1 , .site_bg1').fadeIn()
                $('.gb_01, .popover_m1').click(function(){
                    $('.popover_m1, .site_bg1').fadeOut()
                })
            }
        })        
    }  
    $('#pay_cart').click(function(){
        window.location.href='/src/cart/cart.html?goodsid='+id
    })
    $('#pay_ok').click(function(){
        $.cookie('c','',{path:'/'})
        $.cookie('crowd','',{path:'/'})
    })
     // 数据数组拼接格式：档位ID+名称+图片+单价+购买数量+运费
    if($.cookie('crowd')!=''&&$.cookie('crowd')!=null){
        var list_coo=$.cookie('crowd').split(',') 
        $('.cro_title').text(list_coo[1])
        $('.cro_name').text('').css('height','20px')
        $('.cro_img img').attr('src',list_coo[2])
        $('.cro_mo span').eq(0).text('￥'+list_coo[3]+' x '+list_coo[4])
        $('.cro_mo span').eq(1).text('')
        $('.cro_mo span').eq(2).text('￥'+(parseFloat(list_coo[3])*parseFloat(list_coo[4])).toFixed(2))
        $('.hj').text(list_coo[4]+'件商品'+' , '+'￥'+(parseFloat(list_coo[3])*parseFloat(list_coo[4])).toFixed(2))
        $('.store1 i').text(parseFloat(list_coo[5]).toFixed(2))
        $('.unitPrice').text('￥'+(parseFloat(list_coo[3])*parseFloat(list_coo[4])+parseFloat(list_coo[5])).toFixed(2))
    }


    //众筹结算
    $('#pay').click(function(){
        if(phone_binding!=null&&phone_binding!=''){
            var is_checked=''
            var addressId=site 
            var taxpayerNumber=$('#taxpayerNumber').val();
            var invoiceTitle=''
            $('input[type="radio"]').each(function(index,t){
                if($(t).is(':checked')){
                    is_checked= $(t).val()
                }
            });
            if(is_checked=='2'){
                var invoiceTitle=$('#invoiceTitleOne').val()
                if($('#invoiceTitleOne').val()==''){
                    tc('请填写发票抬头')
                    return false;
                }
            }
            if(is_checked=='3'){
                var invoiceTitle=$('#invoiceTitle').val()
                if($('#invoiceTitle').val()==''){
                    tc('请填写发票抬头')
                    return false;
                }
                if($('#taxpayerNumber').val()==''){
                    tc('请填写纳税人识别号')
                    return false;
                }
            }  
            if($('#checkbox').is(':checked')){
                $.ajax({
                    url:new_url+'shop/crowdorder/createCrowdOrder',
                    data:{token:token,itemId:list_coo[0],buyNum:list_coo[4],message:'',addressId:addressId,invoiceType:is_checked,invoiceTitle:invoiceTitle,taxpayerNumber:taxpayerNumber},
                    dataType:'json',
                    type:'post',
                    success:function(n){
                        function CurentTime(){   
                            var now = new Date();  
                            var year = now.getFullYear();       //年  
                            var month = now.getMonth() + 1;     //月  
                            var day = now.getDate();            //日  
                            var hh = now.getHours();            //时  
                            var mm = now.getMinutes();          //分  
                            var ss = now.getSeconds();           //秒    
                            var clock = year + "-";                               
                            if(month < 10)  
                                clock += "0";    
                            clock += month + "-";   
                            if(day < 10)  
                                clock += "0";      
                            clock += day + " ";   
                            if(hh < 10)  
                                clock += "0";       
                            clock += hh + ":";  
                            if (mm < 10) clock += '0';   
                            clock += mm + ":";   
                            if (ss < 10) clock += '0';   
                            clock += ss;   
                            return(clock);   
                    }  
                        if(n.code=='0'){
                            var str=CurentTime()
                            tc(n.message)
                            $.cookie('c','2',{path:'/'})
                            var l=n.data.payNumber+'&'+n.data.orderList[0].orderPrice+'&'+n.data.orderList[0].orderNo+'&'+n.data.orderList[0].orderId+'&'+str+'&'+n.data.payAmount
                            $.cookie('ok',l,{path:'/'})
                            setTimeout(function(){
                                window.location.href='/src/crowdfunding/pay.html'
                            },200)
                        }
                        else{
                            tc(n.message)
                        }
                    }
                })
            }
            else{
                tc('请同意支持者协议')
                return false
            }
            
        }
        else{
            var this_u=window.location.href
            $('.popover_m1 , .site_bg1').fadeIn()
            $('.gb_01, .popover_m1').click(function(){
                $('.popover_m1, .site_bg1').fadeOut()
            })
        }
    }) 
    
}
else{
    window.location.href='/index.html'
}    
})